<template><% var list=table.columns.filter(item=>{return item.formtype!='none'})%>
  <Form ref="form" :model="form" :rules="rule" :label-width="<%=table.labelWidth%>"><% list.forEach(function(item){if(!item.primaryKey){if(item.formtype=='text'){%>
    <FormItem label="<%=item.remark%>" prop="<%=item.javaProperty%>">
      <Input v-model="form.<%=item.javaProperty%>" placeholder=""></Input>
    </FormItem><%}else if(item.formtype=='textarea'){%>
    <FormItem label="<%=item.remark%>" prop="<%=item.javaProperty%>">
      <Input type="textarea" v-model="form.<%=item.javaProperty%>" placeholder=""></Input>
    </FormItem><%}else if(item.formtype=='radio'){%>
    <FormItem label="<%=item.remark%>" prop="<%=item.javaProperty%>">
      <RadioGroup v-model="form.<%=item.javaProperty%>"><% item.items.forEach(function(itemm){if(typeof(itemm.value)=='number'){%>
          <Radio :label="<%=itemm.value%>"><%=itemm.label%></Radio><%}else{%>
          <Radio label="<%=itemm.value%>"><%=itemm.label%></Radio><%}})%>
      </RadioGroup>
    </FormItem><%}else if(item.formtype=='select'){%>
    <FormItem label="<%=item.remark%>" prop="<%=item.javaProperty%>">
      <Select v-model="form.<%=item.javaProperty%>"><% item.items.forEach(function(itemm){if(typeof(itemm.value)=='number'){%>
          <Option :value="<%=itemm.value%>"><%=itemm.label%></Option><%}else{%>
          <Option value="<%=itemm.value%>"><%=itemm.label%></Option><%}})%>
      </Select>
    </FormItem><%}else if(item.formtype=='selectdict'){%>
    <FormItem label="<%=item.remark%>" prop="<%=item.javaProperty%>">
      <SelectDict v-model="form.<%=item.javaProperty%>" type="<%=item.type%>"></SelectDict>
    </FormItem><%}}})%>
  </Form>
  </template>
<script>
import { save, update, get } from '@/api/<%=moduleName%>/<%=table.tableName.replace("_",".").replace(/_/g,"")%>.service.js'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      form: {
        id: 0,<% list.forEach(function(item,index){if(item.formtype){%>
        <%=item.javaProperty%>: <%if(typeof(item.defaultValue)=='number'){%><%=item.defaultValue%><%}else{%>'<%=item.defaultValue%>'<%}%><%}if(index<list.length-1){%>,<%}})%>
      },<% var listRule=table.columns.filter(item=>{return item.formtype!='none'&&typeof(item.defaultValue)!='number'})%>
      rule: {<% listRule.forEach(function(item,index){if(item.formtype!='none'&&typeof(item.defaultValue)!='number'){%>
        <%=item.javaProperty%>: [
          { required: <%=item.required==true%>, message: '<%=item.remark%>不能为空', trigger: 'blur' }
        ]<%if(index<listRule.length-1){%>,<%}}})%>
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      get({
        id: this.params.id
      }).then(res => {
        this.form = this.$util.copy(res.data, this.form)
      })
    }
  },
  methods: {
    submit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.isEdit) {
              update(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            } else {
              delete this.form.id
              save(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            }
          } else {
            reject(new Error('error'))
          }
        })
      })
    }
  }
}
</script>
